package cn.edu.lingnan.servlet.project;

import cn.edu.lingnan.service.ProjectService;
import cn.edu.lingnan.service.ProjectServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/deleteproject")
public class DeleteProjectServlet extends HttpServlet {
    private ProjectService projectService= new ProjectServiceImp();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] projectIdStrings = request.getParameterValues("projectIds");
        if (projectIdStrings != null) {
            Integer[] projectIds = new Integer[projectIdStrings.length];
            for (int i = 0; i < projectIdStrings.length; i++) {
                projectIds[i] = Integer.parseInt(projectIdStrings[i]);
            }

            int rowsAffected = projectService.deleteProject(projectIds);
            System.out.println("删除的项目数为："+rowsAffected);
            if (rowsAffected > 0) {
                response.sendRedirect("manager_queryAllProjects.jsp"); // 删除成功后的跳转页面
            } else {
                response.sendRedirect("error.jsp"); // 删除失败后的跳转页面
            }
        } else {
            response.sendRedirect("error.jsp"); // 未选中任何项目的跳转页面
        }
    }
}